package Server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

import Game.Challange;

import com.lloseng.ocsf.server.ConnectionToClient;
import com.lloseng.ocsf.server.ObservableServer;
/**
 * The server class
 * @author wat wattanagaroon
 * @author Parinthorn Panya
 * @version 2013/05/10
 *
 */
public class Server extends ObservableServer {

	private int connect = 0;
	private final int maxConnect = 2;
	private int time;
	private String name1;
	private String name2;
	private Integer size;
	private Double time1;
	private Double time2;
	private boolean x1 = false;
	private boolean x2 = false;
	private Random random;
	private Boolean ena;
	/**
	 * The constructor of the server.
	 * @param port of the game
	 * @throws IOException
	 */
	public Server(int port) throws IOException {
		super(port);
		this.listen();
		long seed = System.nanoTime();
		random = new Random(seed);
	}

	@Override
	/**
	 * when the client conneted to the Server 1st time will come in this method
	 */
	protected void clientConnected(ConnectionToClient client) {
		this.sendToAllClients("Please Type Your name");

	}

	/**
	 * when the client disconnect from the server will come in this method
	 */
	@Override
	protected synchronized void clientDisconnected(ConnectionToClient client) {
		this.sendToAllClients(client.getInfo("name") + " has Logout!");
		connect--;
	}

	/**
	 * this method will receive the msg from the client ex.
	 * client.sendToServer(Object n);
	 */
	@Override
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		if (msg.getClass() == String.class) {
			connect++;
			if (connect < maxConnect) {
				client.setInfo("name", msg);
				name1 = msg.toString();
				client.setInfo("player", "1");
				// allPlayer.add(client);
				System.out.println(client.getInfo("name"));
				this.sendToAllClients(client.getInfo("name") + "  Login!");
				try {
					client.sendToClient("Waiting For Player...");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (connect == maxConnect) {
				client.setInfo("name", msg);
				name2 = msg.toString();
				client.setInfo("player", "2");
				// allPlayer.add(client);
				System.out.println(client.getInfo("name"));
				this.sendToAllClients(client.getInfo("name") + "  Login!");

				long seed = System.nanoTime();
				Random random = new Random(seed);

				this.sendToAllClients("p" + "/" + (random.nextInt(200))
						+ "/" + name1 + "/"
						+ name2);
			}

			if (connect > maxConnect) {
				try {
					client.sendToClient("Server is Full.");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, "Server is Full.",
						"WARNING!!!", JOptionPane.WARNING_MESSAGE);
				try {
					client.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		if (msg.getClass() == Double.class) {
			if (client.getInfo("player").equals("1")) {
				time1 = (Double) msg;
				x1 = true;
			} else if (client.getInfo("player").equals("2")) {
				time2 = (Double) msg;
				x2 = true;
			}

			if (x1 && x2) {
				Integer result = CompareTimeofClient(client);
				this.sendToAllClients(result);
				//word number. for the maximum number of the word
				this.sendToAllClients("b" + (random.nextInt(size))); 
				x1 = false;
				x2 = false;
			}

		}
		if(msg.getClass() == Integer.class){
			size = (Integer)msg;
		}
		
		if(msg.getClass() == Boolean.class){
			ena = false;
			this.sendToAllClients(ena);
		}

	}
	/**
	 * The method that compare the time
	 * @param client of the game
	 * @return the number of the player
	 */
	public Integer CompareTimeofClient(ConnectionToClient client) {

		if (time1.doubleValue() > time2.doubleValue())
			return 2;
		if (time2.doubleValue() > time1.doubleValue())
			return 1;
		return 0;

	}

}
